# Below is the interface for Iterator, which is already defined for you.
#
class Iterator:
    def __init__(self, nums):
        """
        Initializes an iterator object to the beginning of a list.
        :type nums: List[int]
        """
        self.nums = nums
        self.index = 0

    def hasNext(self):
        """
        Returns true if the iteration has more elements.
        :rtype: bool
        """
        return self.index < len(self.nums)

    def next(self):
        """
        Returns the next element in the iteration.
        :rtype: int
        """
        tmp = self.index
        self.index += 1
        return self.nums[tmp]


class PeekingIterator:
    def __init__(self, iterator):
        """
        Initialize your data structure here.
        :type iterator: Iterator
        """
        self.iterator = iterator
        self.top = None
        self.hasPeek = False

    def peek(self):
        """
        Returns the next element in the iteration without advancing the iterator.
        :rtype: int
        """
        if not self.hasPeek:
            self.top = self.next()
            self.hasPeek = True
        return self.top

    def next(self):
        """
        :rtype: int
        """
        if self.hasPeek:
            self.hasPeek = False
            return self.top
        return self.iterator.next()

    def hasNext(self):
        """
        :rtype: bool
        """
        if self.hasPeek:
            return True
        return self.iterator.hasNext()


if __name__ == '__main__':
# Your PeekingIterator object will be instantiated and called as such:
    nums = [1,2,3]
    iter = PeekingIterator(Iterator(nums))
    while iter.hasNext():
        val = iter.peek()   # Get the next element but not advance the iterator.
        print(val)
        print(iter.next())         # Should return the same value as [val].
